<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">
  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>
  <link rel="import" href="../../polymer.html">
  <link rel="import" href="utils-elements.html">
</head>
<body>

  <x-content id="elt1">
  </x-content>

  <x-content id="elt2">
    <div></div>
    <div></div>
    <div></div>
  </x-content>

  <x-content-multi id="elt3">
    <span></span>
    <div></div>
    <span></span>
    <div></div>
    <div></div>
    <span></span>
    <span></span>
  </x-content-multi>

  <x-content-mixed id="elt4">
    <span></span>
    <x-content></x-content>
  </x-content-mixed>

  <x-content-mixed id="elt5">
    <x-content-mixed id="elt6"></x-content-mixed>
  </x-content-mixed>

  <x-compose id="elt7">
    <div class="b">b</div>
    <div class="d">d</div>
  </x-compose>

  <script>

    suite('content utils', function() {

      var elt1 = document.querySelector('#elt1');
      var elt2 = document.querySelector('#elt2');
      var elt3 = document.querySelector('#elt3');
      var elt7 = document.querySelector('#elt7');

      test('getContentChildNodes (empty)', function() {
        var nodes = elt1.getContentChildNodes();
        assert.equal(nodes.length, 1, 'should have 1 text node');
      });

      test('getContentChildren (empty)', function() {
        var nodes = elt1.getContentChildren();
        assert.equal(nodes.length, 0, 'should have no children');
      });

      test('getContentChildNodes', function() {
        var nodes = elt2.getContentChildNodes();
        assert.equal(nodes.length, 7, 'should have 7 nodes (text nodes + divs)');
      });

      test('getContentChildren', function() {
        var nodes = elt2.getContentChildren();
        assert.equal(nodes.length, 3, 'should have 3 divs');
      });

      test('getContentChildNodes with selector', function() {
        var nodes = elt3.getContentChildNodes('[select=div]');
        assert.equal(nodes.length, 3, 'should have 3 divs');
        nodes = elt3.getContentChildNodes('[select=span]');
        assert.equal(nodes.length, 4, 'should have 4 spans');
      });

      test('getContentChildren with selector', function() {
        var nodes = elt3.getContentChildren('[select=div]');
        assert.equal(nodes.length, 3, 'should have 3 divs');
        nodes = elt3.getContentChildren('[select=span]');
        assert.equal(nodes.length, 4, 'should have 4 spans');
      });

      test('getContentChildNodes with non-existent selector', function() {
        var nodes = elt3.getContentChildNodes('[dne]');
        assert.equal(nodes.length, 0, 'should find 0 nodes');
      });

      test('getContentChildren with non-existent selector', function() {
        var nodes = elt3.getContentChildren('[dne]');
        assert.equal(nodes.length, 0, 'should find 0 nodes');
      });

      test('getEffectiveChildNodes', function() {
        var nodes = elt7.$.content.getEffectiveChildNodes();
        assert.equal(nodes.length, 14);
      });

      test('getEffectiveChildren', function() {
        var nodes = elt7.$.content.getEffectiveChildren();
        assert.equal(nodes.length, 5);
        assert.ok(nodes[0].classList.contains('a'));
        assert.ok(nodes[1].classList.contains('b'));
        assert.ok(nodes[2].classList.contains('c'));
        assert.ok(nodes[3].classList.contains('d'));
        assert.ok(nodes[4].classList.contains('e'));
      });

      test('getEffectiveTextContent', function() {
        var text = elt7.$.content.getEffectiveTextContent();
        assert.equal(text.replace(/\s/g, ''), 'abcde');
      });

      test('queryAllEffectiveChildren', function() {
        var nodes = elt7.$.content.queryAllEffectiveChildren('div:not(.a)');
        var aNode = elt7.$.content.queryAllEffectiveChildren('.a');
        assert.equal(nodes.length, 4);
        assert.equal(aNode.length, 1);
        assert.ok(nodes[0].classList.contains('b'));
        assert.ok(nodes[1].classList.contains('c'));
        assert.ok(nodes[2].classList.contains('d'));
        assert.ok(nodes[3].classList.contains('e'));
      });


    });

    suite('isLight/Local descendant utils', function() {
      var elt1 = document.querySelector('#elt1');
      var elt4 = document.querySelector('#elt4');
      var elt5 = document.querySelector('#elt5');
      var elt6 = document.querySelector('#elt6');

      test('isLightDescendant is false for self', function() {
        assert.isFalse(elt1.isLightDescendant(elt1));
      });

      test('isLocalDescendant is false for self', function() {
        assert.isFalse(elt1.isLocalDescendant(elt1));
      });

      test('isLightDescendant is true for light children', function() {
        var span = elt4.querySelector('span');
        var customElement = elt4.querySelector('x-content');
        assert.isTrue(elt4.isLightDescendant(span));
        assert.isTrue(elt4.isLightDescendant(customElement));
      });

      test('isLocalDescendant is false for light children', function() {
        var span = elt4.querySelector('span');
        var customElement = elt4.querySelector('x-content');
        assert.isFalse(elt4.isLocalDescendant(span));
        assert.isFalse(elt4.isLocalDescendant(customElement));
      });

      test('isLightDescendant is false for local children', function() {
        assert.isFalse(elt4.isLightDescendant(elt4.localChild));
      });

      test('isLocalDescendant is true for local children', function() {
        assert.isTrue(elt4.isLocalDescendant(elt4.localChild));
      });

      test('isLightDescendant is false for unrelated elements', function() {
        assert.isFalse(elt4.isLightDescendant(elt1));
      });

      test('isLocalDescendant is false for unrelated elements', function() {
        assert.isFalse(elt4.isLocalDescendant(elt1));
      });

      test('isLightDescendant is false for somebody else\'s local child', function() {
        assert.isFalse(elt5.isLightDescendant(elt6.localChild));
      });

      test('isLocalDescendant is false for somebody else\'s local child', function() {
        assert.isFalse(elt5.isLocalDescendant(elt6.localChild));
      });
    });

  </script>

</body>
</html>
